package com.flea.hclv.service.impl;

import com.alibaba.fastjson.JSON;
import com.flea.hclv.constant.HConstant;
import com.flea.hclv.model.dao.HGuideAccountMapper;
import com.flea.hclv.model.entity.HGuideAccount;
import com.flea.hclv.service.HGuideAccountService;
import com.flea.hclv.util.MD5Util;
import com.flea.hclv.util.ServerResponse;
import com.flea.system.platform.session.FleaOrgUtils;
import com.flea.system.utils.FleaStringUtils;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;


/**
 * @program: project
 * @description: 游客账户
 * @author: shiliqiang
 * @create: 2018-11-17 20:53
 * @version: 1.0
 */
@Service
public class HGuideAccountServiceImpl implements HGuideAccountService {

    @Autowired
    private HGuideAccountMapper hGuideAccountMapper;

    @Override
    public ServerResponse<Map<String, String>> updateResetPassword(String username, String password) {
        HGuideAccount account = hGuideAccountMapper.selectByName(username);
        account.setPassword(MD5Util.MD5EncodeUtf8(password.trim()));
        account.setModifyUser(account.getId());
        int row = hGuideAccountMapper.updateByPrimaryKeySelective(account);
        if (row==1){
            return ServerResponse.createBySuccessMessage("重置密码成功");
        }
        return ServerResponse.createByErrorMessage("重置密码失败");
    }

    @Override
    public ServerResponse<Map<String, String>> getAnswer(String username, String answer) {
        HGuideAccount account = hGuideAccountMapper.selectByName(username);
        if (account.getAnswer().equals(answer)){
            return ServerResponse.createBySuccess();
        }
        return ServerResponse.createByErrorMessage("问题回答错误");
    }

    @Override
    public ServerResponse<String> updatePassword(String userId, String oldPass, String newPass) {
        if (StringUtils.isBlank(oldPass)||StringUtils.isBlank(newPass)){
            return ServerResponse.createByErrorMessage("密码不能为空");
        }
//        if (!StringUtils.equals(newPass,confimPass)){
//            return ServerResponse.createByErrorMessage("两次输入密码不一致");
//        }
        if (StringUtils.equals(newPass,oldPass)){
            return ServerResponse.createByErrorMessage("新密码不能原密码与一致");
        }
        HGuideAccount account = hGuideAccountMapper.selectByPrimaryKey(userId);
        if (account==null){
            return ServerResponse.createByErrorMessage("账号不存在");
        }
        String pass = account.getPassword();
        if (!pass.equals(MD5Util.MD5EncodeUtf8(oldPass))){
            return ServerResponse.createByErrorMessage("旧密码不正确");
        }
        account.setPassword(MD5Util.MD5EncodeUtf8(newPass));
        int row = hGuideAccountMapper.updateByPrimaryKeySelective(account);
        if (row==1){
            return ServerResponse.createBySuccessMessage("密码更新成功");
        }
        return ServerResponse.createByErrorMessage("密码更新失败");
    }

    @Override
    public ServerResponse<Map<String,String>> findQuestionByUsername(String username) {

        if (StringUtils.isBlank(username)){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        HGuideAccount account = hGuideAccountMapper.selectByName(username);
        if (account==null){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String question = account.getQuestion();
        Map<String,String> map = Maps.newHashMap();
        map.put("question",question);
        map.put("username",account.getUsername());
        return ServerResponse.createBySuccess(map);
    }

    @Override
    public ServerResponse<HGuideAccount> findByUsernameOrPass(String model) {
        if (StringUtils.isBlank(model)){
            return ServerResponse.createByErrorMessage("model为空");
        }
        HGuideAccount hGuideAccount = JSON.parseObject(model,HGuideAccount.class);
        if (hGuideAccount==null){
            return ServerResponse.createByErrorMessage("参数传递错误");
        }
        String username = hGuideAccount.getUsername();
        if (StringUtils.isBlank(username)){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        String password = hGuideAccount.getPassword();
        if (StringUtils.isBlank(password)){
            return ServerResponse.createByErrorMessage("密码不能为空");
        }
        HGuideAccount account = hGuideAccountMapper.selectByName(username);
        if (account==null){
            return ServerResponse.createByErrorMessage("用户名不存在");
        }
        String md5Pass = MD5Util.MD5EncodeUtf8(password);
        if (!StringUtils.equals(md5Pass,account.getPassword())){
            return ServerResponse.createByErrorMessage("密码错误");
        }
        account.setPassword(StringUtils.EMPTY);
        return ServerResponse.createBySuccess(account);
    }

    @Override
    public ServerResponse<String> save(String username, String password, String phone, String email, String question, String answer) throws Exception {
        if (StringUtils.isBlank(username)){
            return ServerResponse.createByErrorMessage("用户名不能为空");
        }
        HGuideAccount hGuideAccount1 = hGuideAccountMapper.selectByName(username.trim());
        if (hGuideAccount1!=null){
            return ServerResponse.createByErrorMessage("用户名已存在");
        }
        if (StringUtils.isBlank(password)){
            return ServerResponse.createByErrorMessage("密码不能为空");
        }
        if (StringUtils.isBlank(phone)){
            return ServerResponse.createByErrorMessage("电话不能为空");
        }
        HGuideAccount hGuideAccount = hGuideAccountMapper.selectByPhone(phone.trim());
        if (hGuideAccount!=null){
            return ServerResponse.createByErrorMessage("电话号已存在");
        }
        if (StringUtils.isBlank(email)){
            return ServerResponse.createByErrorMessage("email不能为空");
        }
        HGuideAccount hGuideAccount2 = hGuideAccountMapper.selectByEmail(email.trim());
        if (hGuideAccount2!=null){
            return ServerResponse.createByErrorMessage("email已存在");
        }
        if (StringUtils.isBlank(question)){
            return ServerResponse.createByErrorMessage("问题不能为空");
        }
        if (StringUtils.isBlank(answer)){
            return ServerResponse.createByErrorMessage("答案不能为空");
        }
        HGuideAccount account = new HGuideAccount();
        String id = FleaStringUtils.getGenerateUUID();
        account.setId(id);
        account.setCreateUser(id);
        account.setEmail(email);
        account.setUsername(username);
        String md5Pass = MD5Util.MD5EncodeUtf8(password);
        account.setPassword(md5Pass);
        account.setPhone(phone);
        account.setAnswer(answer);
        account.setQuestion(question);
            //设置部门 导游部
            boolean result = FleaOrgUtils.addUserDept(id, HConstant.HCLV_ROOT_GUAI_DEPT_CODE);
            if (!result){
            	throw new Exception("设置部门失败");
             
            }
            //设置为 未审核
            result = FleaOrgUtils.addUserRole(id,HConstant.HCLV_GUAI_CONFIRM_WAIT_ROLE);
            if (!result){
            	throw new Exception("设置角色失败");
            }
     
        int row = hGuideAccountMapper.insertSelective(account);
        if (row==1){
//            StringBuilder content = new StringBuilder();
//            content.append("恭喜您，注册成功");
//            content.append("用户名：");
//            content.append(username);
//            content.append(",");
//            content.append("密码：");
//            content.append(password);
//            content.append("。");
//            content.append("请牢记！");
//            //发送邮件
//            EmailUtil.builder()
//                    .receive(email)
//                    .title("注册成功")
//                    .content(String.valueOf(content))
//                    .build().sendEmail();
            return ServerResponse.createBySuccessMessage("注册成功");

        }
        throw new Exception("注册失败");
       // return ServerResponse.createByErrorMessage("注册失败");
    }

//    private ServerResponse<String> checkField(HGuideAccount hGuideAccount) {
//        if (hGuideAccount==null) {
//            return ServerResponse.createByErrorMessage("传递的参数为空");
//        }
//        String username = hGuideAccount.getUsername();
//        if (StringUtils.isBlank(username)){
//            return ServerResponse.createByErrorMessage("用户名不能为空");
//        }
//        HGuideAccount hGuideAccount1 = hGuideAccountMapper.selectByName(username.trim());
//        if (hGuideAccount1!=null){
//            return ServerResponse.createByErrorMessage("用户名已存在");
//        }
//        String password = hGuideAccount.getPassword();
//        if (StringUtils.isBlank(password)){
//            return ServerResponse.createByErrorMessage("密码不能为空");
//        }
//        String phone = hGuideAccount.getPhone();
//        if (StringUtils.isBlank(phone)){
//            return ServerResponse.createByErrorMessage("电话不能为空");
//        }
//        String email = hGuideAccount.getEmail();
//        if (StringUtils.isBlank(email)){
//            return ServerResponse.createByErrorMessage("email不能为空");
//        }
//
//        HGuideAccount hGuideAccount2 = hGuideAccountMapper.selectByEmail(email.trim());
//        if (hGuideAccount2!=null){
//            return ServerResponse.createByErrorMessage("email已存在");
//        }
//        String question = hGuideAccount.getQuestion();
//        if (StringUtils.isBlank(question)){
//            return ServerResponse.createByErrorMessage("问题不能为空");
//        }
//        String answer = hGuideAccount.getAnswer();
//        if (StringUtils.isBlank(answer)){
//            return ServerResponse.createByErrorMessage("答案不能为空");
//        }
//        return ServerResponse.createBySuccess();
//    }
}
